package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
* @author 31803
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-07-19 10:30:25
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{


    @Autowired
    TbTypeMapper tbTypeMapper;

    @Autowired
    FastFileStorageClient fileStorageClient;

    @Override
    public R getTreeList(TbType type1) {
        QueryWrapper<TbType> wrapper = new QueryWrapper<>();
        //获取所有类型数据
        if(StringUtils.isNotEmpty(type1.getTypeName())){
            wrapper.lambda().like(TbType::getTypeName,type1.getTypeName());
        }
        if(type1.getTypeStatus()!=null){
            wrapper.lambda().eq(TbType::getTypeStatus,type1.getTypeStatus());
        }
        List<TbType> tbTypes = tbTypeMapper.selectList(wrapper);
        List<TbType> tbTypes1 = new ArrayList<>();

        if(StringUtils.isNotEmpty(type1.getTypeName())||type1.getTypeStatus()!=null){
            for (TbType tbType : tbTypes) {
                if(tbType.getTypePid()!=0){
                    TbType tbType1 = tbTypeMapper.selectById(tbType.getTypePid());
//                    List<TbType> tbTypes2 = tbTypeMapper.selectList(new QueryWrapper<TbType>()
//                            .lambda().eq(TbType::getTypePid, tbType.getTypePid()));

                    tbTypes1.add(tbType1);
                    tbTypes1.add(tbType);
//                    tbTypes1.addAll(tbTypes2);
                }
            }
            List<TbType> tbTypes2 = new ArrayList<>(new LinkedHashSet<>(tbTypes1));
            //转换成树形数据
            List<Tree<String>> treeList = TreeUtil.build(tbTypes2, "0", (type, tree) -> {
                tree.setId(type.getTypeId() + "");
                tree.setName(type.getTypeName());
                tree.setParentId(type.getTypePid() + "");
                tree.putExtra("typeImage", type.getTypeImage());
                tree.putExtra("typeTop", type.getTypeTop());
                tree.putExtra("typeStatus", type.getTypeStatus());
                tree.putExtra("createTime", type.getCreateTime());
            });
            return R.OK(treeList);
        }


        //转换成树形数据
        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid() + "");
            tree.putExtra("typeImage", type.getTypeImage());
            tree.putExtra("typeTop", type.getTypeTop());
            tree.putExtra("typeStatus", type.getTypeStatus());
            tree.putExtra("createTime", type.getCreateTime());
        });

      return R.OK(treeList);

    }

    @Override
    public R uploadImages(MultipartFile file) {

        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }

    }

    @Override
    public R getAddType(TbType type) {

        type.setCreateBy("管理员");
        type.setCreateTime(new Date());
        tbTypeMapper.insert(type);

        return R.OK();
    }

    @Override
    public R getTreeselect() {

        List<TbType> tbTypes = tbTypeMapper.selectList(null);

        //转换成树形数据
        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid() + "");
            tree.putExtra("typeImage", type.getTypeImage());
            tree.putExtra("typeTop", type.getTypeTop());
            tree.putExtra("typeStatus", type.getTypeStatus());
            tree.putExtra("value", type.getTypeId());
            tree.putExtra("label", type.getTypeName());
            tree.putExtra("createTime", type.getCreateTime());
        });

        return R.OK(treeList);
    }
}




